package com.ems.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ems.entity.TProductLine;
import com.ems.service.ITProductLineService;
import com.ems.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZH
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/t-product-line")
public class TProductLineController {
    @Autowired
private ITProductLineService itProductLineService;
    //基础部分
    //增删改查部分
    @GetMapping("/list/")
    public ResultUtil list(){
        if (itProductLineService.list().size()>0){
            return ResultUtil.success(itProductLineService.list());
        }else {
            return ResultUtil.fail("获取失败");
        }
    }
    //新增
    @PostMapping("/add")
    public ResultUtil add(@RequestBody TProductLine tProductLine){
        //验证生产线编号是否存在
        tProductLine.getLineNumber();
        long count = itProductLineService.count(new QueryWrapper<TProductLine>().eq("line_number",tProductLine.getLineNumber()));
        if (count>0){
            return ResultUtil.fail("生产线编号已存在");
        }else{
            return ResultUtil.success(itProductLineService.save(tProductLine));
        }
    }
    //修改
    @PutMapping("/update")
    public ResultUtil update(@RequestBody TProductLine tProductLine){
        if (itProductLineService.updateById(tProductLine)){
            return ResultUtil.success("修改成功");
        }else {
            return ResultUtil.fail("修改失败");
        }
    }
    //逻辑删除
    @DeleteMapping("/removeByLogic/{idList}")
    public ResultUtil removeByLogic(@PathVariable("idList") Collection<Integer> idList){
        if (itProductLineService.removeByIds(idList)){
            return ResultUtil.success("删除成功");
        }else {
            return ResultUtil.fail("删除失败");
        }
    }

    //业务部分
    //权限验证
    @PostMapping("/check")
    public ResultUtil check(@RequestParam Integer id){
        if (itProductLineService.check(id).size()>0){
            System.out.println(itProductLineService.check(id)+"返回的数据");
            return ResultUtil.success(itProductLineService.check(id));
        }else {
            return ResultUtil.fail("失败");
        }
    }
    //绑定机器
    @PutMapping("/bindMachine")
    public ResultUtil bindMachine(@RequestBody TProductLine tProductLine){
        if (itProductLineService.bindMachine(tProductLine)){
            return ResultUtil.success("绑定成功");
        }else {
            return ResultUtil.fail("绑定失败");
        }
    }
    //解除机器
    @PutMapping("/unbindMachine")
    public ResultUtil unbindMachine(@RequestBody TProductLine tProductLine){
        if (itProductLineService.unbindMachine(tProductLine)){
            return ResultUtil.success("解除成功");
        }else {
            return ResultUtil.fail("解除失败");
        }

    }

    //修改工序
    @PutMapping("/updateProcess")
    public ResultUtil updateProcess(@RequestBody TProductLine tProductLine){
        if (itProductLineService.updateProcess(tProductLine)){
            return ResultUtil.success("修改成功");
        }else {
            return ResultUtil.fail("修改失败");
        }

    }

    //绑定员工
    @PutMapping("/bindEmployee")
    public ResultUtil bindEmployee(@RequestBody TProductLine tProductLine){
        if (itProductLineService.bindEmployee(tProductLine)){
            return ResultUtil.success("绑定成功");
        }else {
            return ResultUtil.fail("绑定失败");
        }
    }
    //解除员工
    @PutMapping("/unbindEmployee")
    public ResultUtil unbindEmployee(@RequestBody TProductLine tProductLine){
        if (itProductLineService.unbindEmployee(tProductLine)){
            return ResultUtil.success("解除成功");
        }else {
            return ResultUtil.fail("解除失败");
        }
    }
}
